package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HelloJon {
    @Value("${server.port}")
    private String port;
    @XxlJob("demoJobHandler")
    public void helloJob(){
        System.out.println("简单任务执行了。。。。。。。。。。。。"+port);
    }

    @XxlJob("shardingJobHandler")
    public void shardingJobHandler(){
        //分片的参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();

        //业务逻辑
        List<Integer> list=getList();
        for (Integer i : list) {
            if(i % shardTotal ==shardIndex){
                System.out.println("当前第"+shardIndex+"分片执行的任务项为："+i);
            }
        }
    }

    private List<Integer> getList() {
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<10000;i++){
            list.add(i);
        }
        return list;
    }
}
